package com.example.children.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.BehaviorTeacherDTO;
import com.example.children.DTO.HealthCheckTeacherDTO;
import com.example.children.Model.Attendance;
import com.example.children.Model.HealthCheck;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface HealthCheckMapper extends BaseMapper<HealthCheck> {



    @Select("select * from HealthCheck where student_id=#{student_id}") //根据学生id来查找他的所有的体检记录
    List<HealthCheck> selectByStudentId(String student_id);


    //日期查询
    @Select("SELECT * FROM HealthCheck WHERE student_id = #{studentId} AND check_date LIKE CONCAT('%', #{checkDate}, '%') ORDER BY check_date")
    List<HealthCheck> selectByDateSend(HealthCheck healthCheck);



    // 查询总记录数
    @Select("SELECT COUNT(*) FROM HealthCheck s WHERE s.check_id IS NOT NULL")
    int countTotalRecords();

    // 手动分页查询
    @Select("SELECT " +
            "s.student_id AS studentId, s.check_id AS checkId, s.check_date as checkDate,ss.name as studentName, " +
            "s.height, s.weight,s.vision,s.hearing,s.doctor_comments as doctorComments, cls.teacher_id AS teacherId, ct.class_name AS className, c.class_id AS classId, " +
            "t.teacher_name as teacherName,t.teacher_phone as teacherPhone "+
            "FROM HealthCheck s " +
            "JOIN student ss ON s.student_id = ss.id " +
            "left join class c ON s.student_id = c.student_id " +
            "left JOIN classTo ct ON ct.class_id = c.class_id " +
            "left JOIN classteacher cls ON cls.class_id = c.class_id " +
            "left JOIN teacher t ON t.id = cls.teacher_id " +
            "WHERE s.check_id IS NOT NULL AND (cls.headteacher = '班主任' OR cls.headteacher IS NULL) " +
            "ORDER BY s.check_date " +
            "LIMIT #{pageSize} OFFSET #{offset}")
    List<HealthCheckTeacherDTO> GetAllHealthCheckTeacherDTO(@Param("offset") int offset, @Param("pageSize") int pageSize);


    //admin查询
    @Select("SELECT DATE_FORMAT(a.check_date,'%Y-%m-%d') as checkDate,a.doctor_comments as doctorComments,s.id as studentId,s.name as studentName,a.check_id as checkId, " +
            "t.id as teacherId,t.teacher_name as teacherName,t.teacher_phone as teacherPhone,c.class_id as classId, " +
            "ct.class_name as className,a.height,a.weight,a.vision,a.hearing " +
            "FROM HealthCheck a " +
            "JOIN student s ON a.student_id = s.id " +
            "LEFT JOIN class c ON c.student_id = s.id " +
            "LEFT JOIN classTo ct ON c.class_id = ct.class_id " +
            "LEFT JOIN classteacher cls ON cls.class_id = c.class_id " +
            "LEFT JOIN teacher t ON t.id = cls.teacher_id " +
            "WHERE (#{studentId} IS NULL OR a.student_id LIKE CONCAT('%', #{studentId}, '%')) " +
            "AND (#{checkDate} IS NULL OR a.check_date LIKE CONCAT('%', #{checkDate}, '%')) " +
            "AND (#{studentName} IS NULL OR s.name LIKE CONCAT('%', #{studentName}, '%')) " +
            "ORDER BY a.check_date")
    List<HealthCheckTeacherDTO> AdminSelectByDateSend(HealthCheckTeacherDTO healthCheckTeacherDTO);
}
